package com.lap.context.starter.autoconfigure;

import com.lap.context.starter.core.interceptor.LangInterceptor;
import com.lap.context.starter.core.interceptor.RequestIdInterceptor;
import com.lap.context.starter.core.interceptor.UserInterceptor;
import com.lap.context.starter.shared.Constant;
import java.util.Locale;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Slf4j
@AutoConfiguration
public class ContextAutoConfiguration {

  // 国际化
  @Bean
  LocaleResolver localeResolver() {
    SessionLocaleResolver localeResolver = new SessionLocaleResolver();
    localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);

    log.info("Initializer context Lap lang localeResolver ,{}", localeResolver);
    return localeResolver;
  }

  @Bean
  @ConditionalOnMissingBean
  LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
    localeInterceptor.setParamName(Constant.LANG);

    log.info("Initializer context Lap lang localeChangeInterceptor ,{}", localeInterceptor);
    return localeInterceptor;
  }

  @Bean
  LangInterceptor langInterceptor() {
    log.info("Initializer context Lap lang langInterceptor");
    return new LangInterceptor();
  }

  // 其他配置
  @Bean
  UserInterceptor userInterceptor() {
    log.info("Initializer context Lap User Interceptor");
    return new UserInterceptor();
  }

  @Bean
  RequestIdInterceptor requestIdInterceptor() {
    log.info("Initializer context Lap RequestId Interceptor");
    return new RequestIdInterceptor();
  }
}
